/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import dao.DAOGiamGiaAll;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import plugin.DatePicker;
import pojo.*;

/**
 *
 * @author QTuan
 */
public class GiamGia extends javax.swing.JPanel {

    /**
     * Creates new form GiamGiaAll
     */
    private DefaultTableModel model = new DefaultTableModel();
    public GiamGia() {
        initComponents();
        model.addColumn("Id");
        model.addColumn("Ngày bắt đầu");
        model.addColumn("Ngày kết thúc");
        model.addColumn("Phần tram giảm(%)");
        this.jTable1.setModel(model);
        this.jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DAOGiamGiaAll daoGiamgia = new DAOGiamGiaAll();
        List<GiamGiaAll> giamgia = daoGiamgia.getAllGiamGia();
        for(int i=0;i<giamgia.size();i++)
        {
            model.addRow(new Object[]{giamgia.get(i).getId(),giamgia.get(i).getDtimeStart(),giamgia.get(i).getDtimeEnd(),giamgia.get(i).getPhanTramGiam()});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jLabel1.setText("Thời gian khuyến mãi");

        jLabel2.setText("Ngày bắt đầu");

        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField1MouseClicked(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jLabel3.setText("Ngày kết thúc");

        jTextField2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField2MouseClicked(evt);
            }
        });
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField2KeyPressed(evt);
            }
        });

        jLabel4.setText("Phần trăm giảm giá");

        jButton1.setText("Tạo khuyến mãi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Danh sách các đợt khuyến mãi");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel4)))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jTextField2)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                        .addGap(114, 114, 114)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(382, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(155, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        this.jTextField1.setText(new DatePicker(this).setPickedDate());
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseClicked
        this.jTextField1.setText(new DatePicker(this).setPickedDate());
    }//GEN-LAST:event_jTextField1MouseClicked

    private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyPressed
        this.jTextField2.setText(new DatePicker(this).setPickedDate());
    }//GEN-LAST:event_jTextField2KeyPressed

    private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField2MouseClicked
        this.jTextField2.setText(new DatePicker(this).setPickedDate());
    }//GEN-LAST:event_jTextField2MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Integer phantram = TryParseInt(this.jTextField3.getText());
        if(phantram == null)
        {
            JOptionPane.showMessageDialog(null, "Bạn phải nhập 1 số trong khoảng từ 0 đến 100!","Lỗi nhập",JOptionPane.WARNING_MESSAGE);
            this.jTextField3.setText("");
        }else
        {
            if(phantram < 0 || phantram >100)
            {
                JOptionPane.showMessageDialog(null, "Bạn phải nhập 1 số trong khoảng từ 0 đến 100!","Lỗi nhập",JOptionPane.WARNING_MESSAGE);
                this.jTextField3.setText("");
            }else
            {
                if(this.jTextField1.getText().equals("") || this.jTextField2.getText().equals(""))
                {
                    JOptionPane.showMessageDialog(null, "Bạn chưa chọn thời gian khuyến mãi!","Lỗi nhập",JOptionPane.WARNING_MESSAGE);
                }else
                {
                    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                    Date datestart = null;
                    Date dateend = null;
                    try {
 
                        datestart = formatter.parse(this.jTextField1.getText());
                        dateend = formatter.parse(this.jTextField2.getText());
                    
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    //Gia
                    DAOGiamGiaAll daogiamgiaall = new DAOGiamGiaAll();
                    GiamGiaAll giamgia = new GiamGiaAll();
                    giamgia.setDtimeStart(datestart);
                    giamgia.setDtimeEnd(dateend);
                    giamgia.setPhanTramGiam(phantram);
                    daogiamgiaall.setGiamGiaAll(giamgia);
                    for(int i=model.getRowCount()-1;i>=0;i--)
                    {
                        model.removeRow(0);
                    }
                    DAOGiamGiaAll daoview = new DAOGiamGiaAll();
                    List<GiamGiaAll> giamgia1 = daoview.getAllGiamGia();
                    for(int i=0;i<giamgia1.size();i++)
                    {
                        model.addRow(new Object[]{giamgia1.get(i).getId(),giamgia1.get(i).getDtimeStart(),giamgia1.get(i).getDtimeEnd(),giamgia1.get(i).getPhanTramGiam()});
                    }
                    
                }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
public static Integer TryParseInt(String someText) {
        try {
            return Integer.parseInt(someText);
        } catch (NumberFormatException ex) {
        return null;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
